<template>
  <div class="main settle-detail-container">
    <div class="bottom-margin">
      <el-button type="primary" class="back-btn" plain size="medium" @click="$router.push({ name: 'settleList' })">返回</el-button>
    </div>
    <div class="opt-main bottom-margin">
      <div class="opt">结算状态：{{ auditStatusFormat }}</div>
    </div>
    <div v-if="settleDetail.auditStatus == 8 || settleDetail.auditStatus == 7" class="reason">驳回原因：{{ settleDetail.auditRemark }}</div>
    <div class="title bottom-margin">结算信息</div>
    <div class="data-detail">
      <el-row>
        <el-col :span="8">
          <div><span class="field-tip">商户名称：</span>{{ settleDetail.merchantName }}</div>
        </el-col>
        <el-col :span="8">
          <div><span class="field-tip">账单周期：</span>{{ settleDetail.settleCycle }}</div>
        </el-col>
        <el-col :span="8">
          <div><span class="field-tip">账单接收时间：</span>{{ formatDate(settleDetail.billReceiveDate, 'YYYY-MM-DD HH:mm:ss')}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <div><span class="field-tip">本期结算金额：</span>{{ settleDetail.settleAmount }}</div>
        </el-col>
        <el-col :span="8">
          <div><span class="field-tip">本期订单结算金额：</span>{{ settleDetail.orderSettleAmount }}</div>
        </el-col>
        <el-col :span="8">
          <div><span class="field-tip">本期退款金额：</span>{{ settleDetail.refundAmount }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <div><span class="field-tip">上期应结算金额：</span>{{ settleDetail.lastAmount }}</div>
        </el-col>
        <el-col :span="8">
          <div><span class="field-tip">上期复核金额：</span>{{ settleDetail.lastReviewAmount }}</div>
        </el-col>
        <el-col :span="8">
          <div>
            <span class="field-tip">其他调整金额：</span>
            {{ settleDetail.otherAdjustAmount }}
            <el-link v-if="settleDetail.adjustRemark" type="primary" @click="showRemark(settleDetail.adjustRemark)">备注</el-link>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <div class="total"><span class="field-tip total">本期结算合计：</span>{{ settleDetail.totalAmount }}</div>
        </el-col>
      </el-row>
      <div>
        <p><span>小贴士：</span>本期结算合计 = 本期结算金额 + 上期应结算金额</p>
        <p><span>其中：</span> 本期结算金额 = 本期订单结算金额 - 本期退款金额，上期应结算金额 = 上期复核金额 + 其他调整金额</p>
      </div>
    </div>
    <div class="title bottom-margin">
      <el-row :gutter="24">
        <el-col :span="10">
          <div>本期结算金额：{{ settleDetail.settleAmount }}</div>
        </el-col>
        <el-col :span="14">
          <div>本期订单明细</div>
        </el-col>
      </el-row>
    </div>
    <el-table :data="orderList" v-loading="listLoading" header-row-class-name="table-header" element-loading-text="给我一点时间" border highlight-current-row>
        <el-table-column align="center" prop="orderNo" label="订单号"></el-table-column>
        <el-table-column align="center" prop="payTime" label="付款时间">
          <template slot-scope="scope">
            <span>{{formatDate(scope.row.payTime, 'YYYY-MM-DD HH:mm:ss')}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="shippingTime" label="发货时间">
          <template slot-scope="scope">
            <span>{{formatDate(scope.row.shippingTime, 'YYYY-MM-DD HH:mm:ss')}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="goodsName" label="商品名称" min-width="100"></el-table-column>
        <el-table-column align="center" prop="goodsSn" label="商品货号"></el-table-column>
        <el-table-column align="center" prop="lastAmount" label="商家SKU编码"></el-table-column>
        <el-table-column align="center" prop="skuCode" label="平台SKU编码"></el-table-column>
        <el-table-column align="center" prop="goodsNumber" label="数量" min-width="30"></el-table-column>
        <el-table-column align="center" prop="costPrice" label="供货单价"></el-table-column>
        <el-table-column align="center" prop="refundAmount" label="退款金额"></el-table-column>
        <el-table-column align="center" prop="settleAmount" label="结算金额"></el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="listTotal">
  </el-pagination>
    <div v-if="showLastReview" class="title bottom-margin">
      <el-row :gutter="24">
        <el-col :span="10">
          <div>上期复核金额：{{ settleDetail.lastReviewAmount }}</div>
        </el-col>
        <el-col :span="14">
          <div>上期复核明细</div>
        </el-col>
      </el-row>
    </div>
    <el-table v-if="showLastReview" :data="lastReviewList" v-loading="listLoading" header-row-class-name="table-header" element-loading-text="给我一点时间" border highlight-current-row>
        <el-table-column align="center" prop="orderNo" label="订单号"></el-table-column>
        <el-table-column align="center" prop="goodsName" label="商品名称" min-width="100"></el-table-column>
        <el-table-column align="center" prop="goodsSn" label="商品货号"></el-table-column>
        <el-table-column align="center" prop="lastAmount" label="商家SKU编码"></el-table-column>
        <el-table-column align="center" prop="skuCode" label="平台SKU编码"></el-table-column>
        <el-table-column align="center" prop="goodsNumber" label="数量" min-width="30"></el-table-column>
        <el-table-column align="center" prop="costPrice" label="结算单价">
          <template slot-scope="scope">{{ scope.row.adjustSupplyPrice - scope.row.originalSupplyPrice }}</template>
        </el-table-column>
        <el-table-column align="center" prop="settleAmount" label="结算金额">
          <template slot-scope="scope">{{ (scope.row.adjustSupplyPrice - scope.row.originalSupplyPrice) * scope.row.goodsNumber }}</template>
        </el-table-column>
    </el-table>
    <el-pagination
        v-if="showLastReview"
        @size-change="reviewHandleSizeChange"
        @current-change="reviewHandleCurrentChange"
        :current-page.sync="reviewListQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="reviewListQuery.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="reviewListTotal">
  </el-pagination>
  </div>
</template>
<script>
import { settleDetail, settleOrderList, lastReviewList } from '@/api/merchantsys/settle'
export default {
  name: 'settleDetail',
  data() {
    return {
      listQuery: {
        settleNo: null,
        page: 1,
        pageSize: 10
      },
      reviewListQuery: {
        reviewBillId: null,
        pageNo: 1,
        pageSize: 10
      },
      //结算单详情
      settleDetail: {},
      //本期订单明细
      orderList: [],
      //上期复核明细
      lastReviewList: [],
      reviewListTotal: 0,
      listLoading: false,
      listTotal: 0,
      accountIdList: [],
      auditStatus: [
        { label: '全部', value: null },
        { label: '待确认', value: 1 },
        { label: '已确认', value: 2 },
        { label: '待结算', value: 3 },
        { label: '已结算', value: 4 },
        { label: '待复核', value: 5 },
        { label: '复核通过', value: 6 },
        { label: '复核驳回', value: 7 },
        { label: '结算驳回', value: 8 }
      ]
    }
  },
  computed: {
    auditStatusFormat() {
      return ['-', '待确认', '已确认', '待结算', '已结算', '待复核', '复核通过', '复核驳回', '结算驳回'][this.settleDetail.auditStatus]
    },
    showLastReview() {
      return this.reviewListQuery.reviewBillId
    }
  },
  created() {
    this.fetchSettleDetail()
  },
  methods: {
    //结算单详情
    fetchSettleDetail() {
      let settleNo = this.$route.params.settleNo
      settleDetail({ settle_no: settleNo }).then(res=>{
        let data = res.data
        this.listQuery.settleNo = settleNo
        this.settleDetail = data
        this.reviewListQuery.reviewBillId = data.reviewBillId
        //本期订单明细
        this.getList()
        //上期复核明细
        this.fetchLastReivewList()
      })
      
    },
    //上期复核明细
    fetchLastReivewList() {
      if (!this.reviewListQuery.reviewBillId) {
        return
      }
      lastReviewList({ review_bill_id: this.reviewListQuery.reviewBillId, page: this.reviewListQuery.pageNo, page_size: this.reviewListQuery.pageSize }).then(res=>{
        let data = res.data
        this.lastReviewList = data.data
        this.reviewListTotal = data.total
        this.listLoading = false;
      }).catch(err=>{
        this.lastReviewList = [];
        this.reviewListTotal = 0;
        this.listLoading = false;
      })
    },
    //金额调整备注
    showRemark(remark) {
      this.$alert('备注：' + remark, '其他调整金额', {
        confirmButtonText: '确定'
      })
    },
    //请求商家列表数据
    getList() {
        this.listLoading = true;
        settleOrderList({ settle_no: this.listQuery.settleNo, page: this.listQuery.page, page_size: this.listQuery.pageSize }).then(res => {
            this.orderList = res.data.data;
            this.listTotal = res.data.total;
            this.listLoading = false;
        }).catch(err => {
            this.orderList = [];
            this.listTotal = 0;
            this.listLoading = false;
        })
    },
    //切换每页条数
    handleSizeChange (value) {
        this.listQuery.pageSize = value;
        this.getList()
    },
    // 切换分页
    handleCurrentChange(page) {
        this.listQuery.page = page;
        this.getList();
    },
    //切换每页条数
    reviewHandleSizeChange (value) {
        this.reviewListQuery.pageSize = value;
        this.fetchLastReivewList()
    },
    // 切换分页
    reviewHandleCurrentChange(page) {
        this.reviewListQuery.page = page;
        this.fetchLastReivewList();
    }
  }
}
</script>

<style lang="scss">
  .settle-detail-container{
  padding: 20px;
  .bottom-margin{
    margin-bottom: 10px;
  }
  .back-btn{
    width: 130px;
  }
  .opt-main{
    width: 100%;
    display: flex;
    justify-content: space-between;
    line-height: 30px;
    .opt{
      display: inline-block;
    }
    .opt-btn{
      width: 130px;
    }
  }
  .title{
    font-weight: bold;
    font-size: 18px;
    width: 100%;
  }
  .data-detail{
    margin-bottom: 30px;
    .el-row{
      margin-bottom: 20px;
    }
    .field-tip{
      display: inline-block;
      width: 150px;
      text-align: right;
    }
    p{
      line-height: 25px;
      font-size: 14px;
        color: gray;
      span{
        display: inline-block;
        width: 60px;
        text-align: right;
      }
    }
    .total{
      font-size: 16px;
      color: black;
      font-weight: bold;
    }
  }
  .reason{
    margin-bottom: 20px;
    color: red;
  }
}
</style>